/***************************************************************************
 创建者：华磊
 开始时间：         : 2020.12.12
 copyright            : (C) 深圳市华友高科有限公司
 修改说明：（每次有修改就添加一条，带有 修改人，修改时间，修改描述）

 ***************************************************************************
 *   if条件指令属性编辑视窗测试用代理类
*                                                                         *
 ***************************************************************************/
#include "ifinstructiondelegate.h"
#include <QDebug>
#include "../CommunicateDelegate/communicatenormaldelegate.h"
#include <QMessageBox>
#include "globaldata.h"
#include "../LabelJmpView/progromlistview.h"

IfInstructionDelegate::IfInstructionDelegate(GlobalData *globalDataIn, CommunicateNormalDelegate *communicateNormalDelegateIn,
                                             ProgromListView *progromListViewIn, QWidget *parent):
    QObject(parent)
{
    globalData=globalDataIn;
    communicateNormalDelegate=communicateNormalDelegateIn;
    progromListView=progromListViewIn;

    connect(progromListView,SIGNAL(getSelectFileName(QString)),this,SLOT(showSelectFileName(QString)));
}

void IfInstructionDelegate::initialView()
{
    ifInstructionView->ifInstruction = "IF";
    ifInstructionView->ifCondition = "R[1]>R[2]";
    ifInstructionView->dealWith = " JMP LBL";
    ifInstructionView->dealWithFlag = 0;

    ifInstructionView->callLocation = "./";
    ifInstructionView->callProgram = "file";
    ifInstructionView->labelStr = "";
    ifInstructionView->lblDataType = 0;

    ifInstructionView->note = "";

    //ifInstructionView->updateView();
}



void IfInstructionDelegate::selectJmpSlot()
{
    ifInstructionView->dealWithFlag = 0;
    qDebug() << "select JMP";
}

void IfInstructionDelegate::selectCallSlot()
{
    ifInstructionView->dealWithFlag = 1;
    qDebug() << "select CALL";
}

//void IfInstructionDelegate::browseCallProgramSlot(QString callLocation)
//{
//    QFileDialog dialog(ifInstructionView,"");
//    dialog.resize(580,400);
//    dialog.setAcceptMode(QFileDialog::AcceptOpen);
//    dialog.setNameFilter("TXT(*.txt)");
//    dialog.setDirectory(callLocation);

//    QStringList files = dialog.selectedFiles();
//    if(!files.isEmpty())
//    {
//        if(dialog.exec() == QFileDialog::Accepted)
//        {
//            qDebug()<<"ok";
//            QStringList name = dialog.selectedFiles();

//            if(1 == name.size()){
//                QStringList nameList = name[0].split("/");

//                ifInstructionView->callProgram = nameList[nameList.size()-1];
//                //ifInstructionView->updateView();
//            }
//        }
//        else
//        {
//            qDebug()<<"close";
//        }
//    }
//}

void IfInstructionDelegate::cleanNoteSlot()
{
    ifInstructionView->note.clear();
    qDebug() << "clear note";
}

void IfInstructionDelegate::finishEditInstructionSlot()
{
    ifInstructionView->ifInstruction = "IF ";
    if(0 == ifInstructionView->dealWithFlag)
    {
        ifInstructionView->dealWith = "JMP LBL";

        if(0 == ifInstructionView->lblDataType)
        {
            ifInstructionView->dealWith += "[" + ifInstructionView->labelStr + "]";
        }
        else
        {
            ifInstructionView->dealWith = ifInstructionView->dealWith + "[R[" + ifInstructionView->labelStr + "]]";
        }
    }
    else if(1 == ifInstructionView->dealWithFlag)
    {
        ifInstructionView->dealWith = "CALL " ;
        ifInstructionView->dealWith = ifInstructionView->dealWith + ifInstructionView->callProgram ;
    }

   ifInstructionView->ifInstruction += ifInstructionView->ifCondition + ", " + ifInstructionView->dealWith + "; "
           + "//" + ifInstructionView->note;
}

void IfInstructionDelegate::showSelectFileName(QString fileName)
{
    ifInstructionView->setProgromFileName(fileName);
}

void IfInstructionDelegate::browseCallProgramSlot(QString oldProgramNameIn)
{
    //todo hualei
    progromListView->show();
    qDebug() << "调用程序";
    int returnFlag;
    std::vector <std::string> fileList;
    communicateNormalDelegate->getRobotProgramFileList(globalData->currentRobotID, fileList, returnFlag);
    if(returnFlag == 0)
    {
        // 获取文件成功
        qDebug()<< "文件列表个数：" << fileList.size();
        QStringList fileList1;
        for(int i = 0; i < (int)fileList.size(); i++)
        {
           fileList1.append(QString::fromStdString(fileList.at(i)));
        }

        progromListView->setProgramFileList(fileList1);
        progromListView->setSelectFile(oldProgramNameIn);
    }
    // 通信失败
    else
    {
         QMessageBox::information(NULL, "Information", "Gsoap获取远程程序文件列表失败！", QMessageBox::Yes );
    }

//                    labelJmpView->callProgram = nameList[nameList.size()-1];
//                    labelJmpView->updateView();


}

